#!/usr/bin/perl

use strict;
use warnings;

unless ($#ARGV == 1) {
	print "usage: $0 outfile nfile\n";
	exit 1;
}

my $ofname = $ARGV[0];
my $ifname = $ARGV[1];

print $ifname."\n";
print "$ofname\n";

open FIN, "<$ifname";
my $vardef = <FIN>;
open FOUT, ">$ofname";

print FOUT <<END;
#include "propasm-tbl.h"

opcode_desc propeller_assembly[] = {
END
while (my $line = <FIN>) {
	$line =~ s/[#;].*//;
	next unless length($line)>1;
	my @row = split /\s+/, $line;
	my $dest_type;
	my $src_type;
	$dest_type = 'FULL' if ($row[2] eq 'd');
	$dest_type = 'NOARG' if ($row[2] eq '-');
	$dest_type = 'COMPILED' if ($row[2] eq '?');

	$src_type = 'FULL' if ($row[3] eq 's');
	$src_type = 'NOARG' if ($row[3] eq '-');
	$src_type = 'COMPILED' if ($row[3] eq '?');
	$src_type = 'STATIC' if length($row[3])>1;
	my $src_static = 0;
	$src_static = $row[3] if length($row[3])>1;

	my $z = $row[4];
	my $c = $row[5];

	print FOUT "\t{";
	print FOUT "\"$row[0]\", $row[1], $dest_type, $src_type, $src_static, $z, $c";
	print FOUT "},\n";
}
print FOUT "};\n";

close FIN;
close FOUT;

